package com.oocl.action;

import java.util.Date;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerManager;
import com.oocl.service.impl.CustomerManagerFactory;
@Controller
@RequestMapping("/")
public class CustomerAction {
	private CustomerManager manager;

	public CustomerAction() {
		this.manager = CustomerManagerFactory.getInstance();
	}
	@RequestMapping(value="/addCustomer",method=RequestMethod.POST)
	public String addCustomer(Customer c){
		System.out.println(c);
		manager.addCustomer(c);
		return "redirect:/findAll";
	}
	@RequestMapping(value="/findAll")
	public String findAll(Model model){
		List<Customer> customers = manager.findAll();
		model.addAttribute("customers",customers);
		return "showCustomers";
	}
	@ResponseBody
	@RequestMapping(value="/addCustomerByAjax",consumes="application/json")
	public List<Customer> addCustomerByAjax(@RequestBody Customer c,Model model){
		System.out.println(c);
		manager.addCustomer(c);
		List<Customer> customers = manager.findAll();
		return customers;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delete",consumes="application/json")
	public List<Customer> ajaxDelete(@RequestBody Customer c,Model model){
		System.out.println(c);
		manager.deleteCustomer(c);
		List<Customer> customers = manager.findAll();
		return customers;
	}
	
	@ResponseBody
	@RequestMapping(value="/find",consumes="application/json")
	public Customer ajaxFind(@RequestBody Customer c,Model model){
		Customer customer = manager.findCustomer(c);
		return customer;
	}
	
	@ResponseBody
	@RequestMapping(value="/update",consumes="application/json")
	public List<Customer> ajaxUpdate(@RequestBody Customer c,Model model){
		manager.updateCustomer(c);
		List<Customer> customers = manager.findAll();
		return customers;
	}

	
}
